<template>
  <div>
    <el-dialog :title="titleName" :visible="show" :modal-append-to-body="false" :before-close="cmdClose" center @open="cmdInit" @opened="cmdOpened" width="800px" top="10vh">
      <div>
        <el-form label-width="100px">
          <el-form-item label="当前节点名称">
            <el-input v-model="nodeName" readonly style="width: 250px"></el-input>
          </el-form-item>
        </el-form>
        <div>当前已选择:{{currentSelect}}</div>
        <el-table ref="multipleTable" :data="tableData" stripe border style="width: 100%" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center"></el-table-column>
          <el-table-column prop="name" label="姓名" width="120" align="center"></el-table-column>
          <el-table-column prop="phone" label="电话" align="center"></el-table-column>
        </el-table>
        <div class="paginationStyle">
          <el-pagination v-if="total>0" background layout="total, prev, pager, next, jumper" :page-size="pageSize" @current-change="handleCurrentChange" :total="total"></el-pagination>
        </div>
      </div>
      <div slot="footer" class="dialog-footer footBtn">
        <el-button type="primary" @click="cmdSubmit">确定</el-button>
        <el-button type="primary" @click="cancel">取消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getImplmentNodes, getMembers, sysSaveOne } from '@/api/api'
import swal from 'sweetalert'
import _ from 'underscore'
export default {
  props: {
    show: {
      type: Boolean,
      default: false
    },
    node: {
      type: String,
      default: ''
    },
    content: {
      type: Object,
      defalut: {}
    }
  },
  data () {
    return {
      titleName: '',
      nodeName: '',
      author: '',
      tableData: [],
      pageSize: 10,
      pageNum: 0,
      total: 0,
      selectJobaccounts: '',
      currentSelect: ''
    }
  },
  methods: {
    cmdInit () {
      this.orunid = this.content.orunid
      this.nodeName = this.content.nodeName
      this.author = this.content.author

      this.nodeNames()
      // 获取人员表格数据
      this.getMembersData()
    },
    cmdOpened () {
      // 显示表格默认选中
      let curArr = []
      if (this.author) {
        let arr = this.author.split(',')
        arr.forEach(item => {
          this.tableData.forEach(item2 => {
            if (item2.jobAccount == item) {
              curArr.push(item2)
            }
          })
        })
      }

      this.tableData.forEach(item => {
        if (curArr.length > 0) {
          curArr.forEach(item2 => {
            if (item == item2) {
              this.$refs.multipleTable.toggleRowSelection(item)
            }
          })
        }
      })
    },
    cmdClose () {
      // 清除表格选项数据
      this.$refs.multipleTable.clearSelection()
      this.selectJobaccounts = ''
      this.currentSelect = ''
      this.$emit('closeDlg')
    },
    nodeNames () {
      switch (this.node) {
        case 'config1':
          this.titleName = '修改实施方案系统节点表';
          break;
        case 'config2':
          this.titleName = '修改设计方案系统节点表';
          break;
        case 'config3':
          this.titleName = '添加招投标流程节点表';
          break;
        default:
          this.titleName = '修改系统节点表';
      }
    },
    getMembersData () {
      let params = {
        pageNum: this.pageNum,
        pageSize: this.pageSize,
        status: '1',
      }
      getMembers(params).then((res) => {
        // console.log(res)
        if (res.code == 200) {
          this.tableData = res.data.content
          this.total = res.data.totalElements
        } else {
          swal('错误', res.message || '网络连接错误', 'error')
        }
      }).catch((err) => {
        swal('错误', '网络连接错误', 'error')
      })
    },
    handleCurrentChange (val) {
      this.pageNum = val - 1
      this.getMembersData()
    },
    // 获取选择人员
    handleSelectionChange (val) {
      // console.log(val)
      let jobaccount = ''
      let name = ''
      if (val.length > 0) {
        val.forEach(item => {
          jobaccount = jobaccount + item.jobAccount + ','
          name = name + item.name + ','
        })
      }
      if (jobaccount.substr(jobaccount.length - 1, 1) == ',') {
        jobaccount = jobaccount.substr(0, jobaccount.length - 1)
      }
      if (name.substr(name.length - 1, 1) == ',') {
        name = name.substr(0, name.length - 1)
      }
      this.selectJobaccounts = jobaccount
      this.currentSelect = name
    },
    cmdSubmit: _.debounce(function () {
      if (!this.selectJobaccounts) {
        swal('提示', '请最少选择一个人员', 'warning')
        return false
      }
      let msg = '是否确认提交!'
      this.confirm(msg, () => {
        let params = {
          orunid: this.orunid,
          author: this.selectJobaccounts,
          authorName: this.currentSelect
        }
        // console.log(params)
        sysSaveOne(params).then(res => {
          if (res.code == 200) {
            this.alertok('提交成功', () => {
              this.cmdClose()
            })
          } else {
            swal('错误', res.message || '网络连接错误', 'warning')
          }
        }).catch((err) => {
          swal('错误', '网络连接错误', 'error')
        })
      })
    }, 800),
    cancel () {
      this.cmdClose()
    }
  }
}
</script>
<style scoped>
</style>
